package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        //向套餐表插入数据
        setmealMapper.insert(setmeal);

        //获取insert语句生成的主键值
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishs =setmealDTO.getSetmealDishes();
        setmealDishs.forEach(setmealDish ->{
            //将套餐id保存在套餐关系表中
            setmealDish.setSetmealId(setmealId);
        });
        //保存套餐和菜品的关联关系 动态sql批量插入
        setmealDishMapper.insertBatch(setmealDishs);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        //1.先判断删除的套餐是否在售
        for(Long id:ids){
            //2.如果有在售就不能删除，抛出异常
            int status = setmealMapper.queryStatus(id);
            if(status==1){
                //当前菜品处于起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

            //3.没有在售，则删除数据
            //4.先删套餐菜品关系表，再删套餐表
            setmealDishMapper.deleteBySetmealIds(ids);
            setmealMapper.delete(ids);
    }

    /**
     * 起售或者停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，要判断套餐是否有停售菜品
        if(status.equals(StatusConstant.ENABLE)){
            //根据套餐id查询菜品信息
            List<Dish> dishList=dishMapper.getBySetmealId(id);
            if(dishList !=null && dishList.size()>0){
                dishList.forEach(dish->{
                    //套餐中包含菜品，如果菜品被停售，则抛出异常
                    if(StatusConstant.DISABLE.equals(dish.getStatus())){
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        //停售套餐
        Setmeal setmeal=Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public SetmealVO getSetmealById(Long id) {
        //1.根据id查询setmeal中的套餐信息
        Setmeal setmeal=setmealMapper.getSetmealById(id);

        //2.查询套餐中的菜品信息，根据setmeal_dish表找
        List<SetmealDish> setmealDishList = setmealDishMapper.getSetmealDishBysetmealId(id);

        //3.封装SetmealVO
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishList);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //将setmealDTO数据拆分 出setmeal
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //1.先修改setmeal表的套餐数据，
        setmealMapper.update(setmeal);

        //2.再修改setmeal_dish表中套餐对应的菜品
        //2.1先删再加
        List<SetmealDish> setmealDishs=setmealDTO.getSetmealDishes();
        if(setmealDishs !=null && setmealDishs.size()>0){
            for(SetmealDish setmealDish:setmealDishs){
                setmealDish.setSetmealId(setmeal.getId());
            }
        }
        //删
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //加
        setmealDishMapper.insertBatch(setmealDishs);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
